package com.tanhua.app.controller;

import com.tanhua.app.Service.CommentsService;
import com.tanhua.app.Service.MessagesService;
import com.tanhua.app.Service.SmallVideosService;
import com.tanhua.model.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.ws.rs.POST;
import java.util.Map;

@RestController
@RequestMapping("/smallVideos")
public class SmallVideosController {
    @Autowired
    private SmallVideosService videosService;
    @Autowired
    private CommentsService commentsService;
    @Autowired
    private MessagesService messagesService;

//    视频上传
//    POST  /smallVideos
    @PostMapping
    public ResponseEntity saveVideos(MultipartFile videoThumbnail,MultipartFile videoFile) throws Exception {
        videosService.saveVideos(videoThumbnail,videoFile);
        return ResponseEntity.ok(null);
    }

//    小视频列表
//    GET   /smallVideos
    @GetMapping
    public ResponseEntity queryVideoList(@RequestParam(defaultValue = "1") Integer page
                                         ,@RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pr = videosService.queryVideoList(page,pagesize);
        return ResponseEntity.ok(pr);
    }

//    视频点赞
//    POST  /smallVideos/:id/like
    @PostMapping("/{id}/like")
    public ResponseEntity videoLike(@PathVariable("id") String videoId){
        Integer likeCount = videosService.videoLike(videoId);
        return ResponseEntity.ok(likeCount);
    }

//    视频点赞 - 取消
//    POST  /smallVideos/:id/dislike
    @PostMapping("/{id}/dislike")
    public ResponseEntity videoDislike(@PathVariable("id") String videoId){
        Integer likeCount = videosService.videoDislike(videoId);
        return ResponseEntity.ok(likeCount);
    }

//    评论列表
//    GET   /smallVideos/:id/comments
    @GetMapping("/{id}/comments")
    public ResponseEntity commentsList(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize
                                        ,@PathVariable("id") String videoId){
        PageResult pr = videosService.findComments(page,pagesize,videoId);
        return ResponseEntity.ok(pr);
    }

//    评论发布
//    POST  /smallVideos/:id/comments
    @PostMapping("/{id}/comments")
    public ResponseEntity saveComments(@RequestBody Map<String,String> map
            ,@PathVariable("id") String videoId){
        String comment = map.get("comment");
        videosService.saveComments(videoId,comment);
        return ResponseEntity.ok(null);
    }

//    评论点赞
//    POST  /smallVideos/comments/:id/like
    @PostMapping("/comments/{id}/like")
    public ResponseEntity likeComments(@PathVariable("id") String commentId){
        Integer likeCount = commentsService.likeComments(commentId);
        return ResponseEntity.ok(likeCount);
    }

//    评论点赞 = 取消
//    POST  /smallVideos/comments/:id/dislike
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity dislikeComments(@PathVariable("id") String commentId){
        Integer likeCount = commentsService.dislikeComments(commentId);
        return ResponseEntity.ok(likeCount);
    }

//    视频用户关注
//    POST  /smallVideos/:uid/userFocus
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity userFocus(@PathVariable("uid") String uid){
        messagesService.userFocus(Long.parseLong(uid));
        return ResponseEntity.ok(null);
    }

//    视频用户关注 - 取消
//    POST  /smallVideos/:uid/userUnFocus
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable("uid") String uid){
        messagesService.userUnFocus(Long.parseLong(uid));
        return ResponseEntity.ok(null);
    }
}
